package com.zhf.douyinvideo.fragment;

import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewParent;
import android.widget.FrameLayout;

import com.bumptech.glide.Glide;
import com.zhf.douyinvideo.R;
import com.zhf.douyinvideo.adapter.VideoLeftAdapter;
import com.zhf.douyinvideo.base.BaseFragment;
import com.zhf.douyinvideo.bean.VideoBean;
import com.zhf.douyinvideo.listener.*;
import com.zhf.douyinvideo.util.DataUtil;
import com.zhf.douyinvideo.video.VideoController;
import com.zhf.douyinvideo.listener.OnViewPagerListener;
import com.zhf.videoplayer.view.VideoView;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class VideoLeftFragment extends BaseFragment {

    private VideoView mVideoView;
    private VideoController mVideoController;
    private int mCurrentPosition;
    private RecyclerView mRecyclerView;
    private List<VideoBean> mVideoList;

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_video;
    }

    @Override
    protected void initWidget(@NotNull View root) {
        super.initWidget(root);
        mVideoView = new VideoView(getActivity());
        mVideoView.setLooping(true);
        mVideoController = new VideoController(getActivity());
        mVideoView.setVideoController(mVideoController);
        mRecyclerView = root.findViewById(R.id.rv);

        mVideoList = DataUtil.getTikTokVideoList();
        VideoLeftAdapter tikTokAdapter = new VideoLeftAdapter(mVideoList, getActivity());
        ViewPagerLayoutManager layoutManager = new ViewPagerLayoutManager(getActivity(), OrientationHelper.VERTICAL);

        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(tikTokAdapter);
        layoutManager.setOnViewPagerListener(new OnViewPagerListener() {
            @Override
            public void onInitComplete() {
                //自动播放第一条
                startPlay(0);
            }

            @Override
            public void onPageRelease(boolean isNext, int position) {
                if (mCurrentPosition == position) {
                    mVideoView.release();
                }
            }

            @Override
            public void onPageSelected(int position, boolean isBottom) {
                if (mCurrentPosition == position) return;
                startPlay(position);
                mCurrentPosition = position;
            }
        });

    }


    private void startPlay(int position) {
        View itemView = mRecyclerView.getChildAt(0);
        FrameLayout frameLayout = itemView.findViewById(R.id.container);
        Glide.with(this)
                .load(mVideoList.get(position).getThumb())
                .placeholder(android.R.color.white)
                .into(mVideoController.getThumb());
        ViewParent parent = mVideoView.getParent();
        if (parent instanceof FrameLayout) {
            ((FrameLayout) parent).removeView(mVideoView);
        }
        frameLayout.addView(mVideoView);
        mVideoView.setUrl(mVideoList.get(position).getUrl());
        mVideoView.setScreenScale(VideoView.SCREEN_SCALE_CENTER_CROP);
        mVideoView.start();
    }

    @Override
    public void onPause() {
        super.onPause();
        mVideoView.pause();
    }

    @Override
    public void onResume() {
        super.onResume();
        mVideoView.resume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // 释放播放器资源
//        mVideoView.release();
    }



}
